#include "stdafx.h"
#include "pyhgeCore.h"
#include "pyhgeFont.h"
//TODO: implement this in python
PyTypeObject FontType=
{
	PyObject_HEAD_INIT(NULL)
	0,                         /*ob_size*/
	"hge.Font",             /*tp_name*/
	sizeof(pyFont), /*tp_basicsize*/
	0,                         /*tp_itemsize*/
	0,                         /*tp_dealloc*/
	0,                         /*tp_print*/
	0,                         /*tp_getattr*/
	0,                         /*tp_setattr*/
	0,                         /*tp_compare*/
	0,                         /*tp_repr*/
	0,                         /*tp_as_number*/
	0,                         /*tp_as_sequence*/
	0,                         /*tp_as_mapping*/
	0,                         /*tp_hash */
	0,                         /*tp_call*/
	0,                         /*tp_str*/
	0,                         /*tp_getattro*/
	0,                         /*tp_setattro*/
	0,                         /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,        /*tp_flags*/
	"Sprite(filename,bMipmap=False) -> Font Object\n"
	"create and initalize a hgeFont object",/* tp_doc */
};
static void
Font_dealloc(PyObject * _self)
{
	pyFont * self = (pyFont*)_self;
	delete self->phgeFont;
	self->ob_type->tp_free((PyObject*)self);
}

static PyObject *
Font_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	pyFont * self = (pyFont*)type->tp_alloc(type, 0);
	if (!self) return NULL;
	self->phgeFont = NULL;
	return (PyObject*)self;
}

static int
Font_init(PyObject *_self, PyObject *args, PyObject *kwds)
{
	PyObject * pMipmap = Py_False;
	const char * filename;
	pyFont * self = (pyFont*)_self;
	static char *kwlist[] = {"filename","bMipmap",0};
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|O:Font", kwlist,&filename,&pMipmap))
		return -1;
	int bval = PyObject_IsTrue(pMipmap);
	if (bval == -1) return -1;
	delete self->phgeFont;
	self->phgeFont = new hgeFont(filename,bval?true:false);
	return 0;
}

static PyObject *
Font_repr(PyObject * _self)
{
	pyFont * self = (pyFont*)_self;
	return PyString_FromFormat("<Font at %p>",self->phgeFont);
}

hgeFont * Font_GetPtr(PyObject * obj)
{
	if (!PyObject_TypeCheck(obj,&FontType))
	{
		PyErr_SetString(PyExc_TypeError,"Font object is required.");
		return 0;
	}
	pyFont * ptr = (pyFont*)obj;
	if (ptr->phgeFont == NULL)
	{
		PyErr_SetString(PyExc_ValueError,"NULL Font.");
		return 0;
	}
	return ptr->phgeFont;
}

/*
Render
*/
DEFINE_PYTHON_FUNCTION(
"F.Render(x,y,align,text) -> None\n"
"Renders the text string to the screen.",
METH_VARARGS,Font_Render)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float x,y;
	int align;
	const char * text;
	if (!PyArg_ParseTuple(args,"ffis",&x,&y,&align,&text))
		return 0;
	fnt->Render(x,y,align,text);
	Py_RETURN_NONE;
}
/*
SetColor
SetZ
SetBlendMode
SetScale
SetProportion
SetRotation
SetTracking
SetSpacing
*/
DEFINE_PYTHON_FUNCTION(
"F.SetColor(color) -> None\n"
"Sets the font tint and alpha. ",
METH_VARARGS,Font_SetColor)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	DWORD val;
	if (!PyArg_ParseTuple(args,"I:SetColor",&val))
		return 0;
	fnt->SetColor(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetZ(z) -> None\n"
"Sets the font Z-order. ",
METH_VARARGS,Font_SetZ)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetZ",&val))
		return 0;
	fnt->SetZ(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetBlendMode(blend) -> None\n"
"Sets the font Z-order. ",
METH_VARARGS,Font_SetBlendMode)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	int val;
	if (!PyArg_ParseTuple(args,"i:SetBlendMode",&val))
		return 0;
	fnt->SetBlendMode(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetScale(scale) -> None\n"
"Sets the font scaling.  ",
METH_VARARGS,Font_SetScale)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetScale",&val))
		return 0;
	fnt->SetScale(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetProportion(prop) -> None\n"
"Sets the font proportion.",
METH_VARARGS,Font_SetProportion)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetProportion",&val))
		return 0;
	fnt->SetProportion(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetRotation(rot) -> None\n"
"Sets the letters rotation.",
METH_VARARGS,Font_SetRotation)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetRotation",&val))
		return 0;
	fnt->SetRotation(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetTracking(tracking) -> None\n"
"Sets the font tracking.",
METH_VARARGS,Font_SetTracking)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetTracking",&val))
		return 0;
	fnt->SetTracking(val);
	Py_RETURN_NONE;
}
DEFINE_PYTHON_FUNCTION(
"F.SetSpacing(spacing) -> None\n"
"Sets the font line spacing. ",
METH_VARARGS,Font_SetSpacing)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	float val;
	if (!PyArg_ParseTuple(args,"f:SetSpacing",&val))
		return 0;
	fnt->SetSpacing(val);
	Py_RETURN_NONE;
}
/*
GetColor
GetZ
GetBlendMode
GetScale
GetProportion
GetRotation
GetTracking
GetSpacing
*/
DEFINE_PYTHON_FUNCTION(
"F.GetColor() -> int\n"
"Returns the current font color.",
METH_NOARGS,Font_GetColor)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyInt_FromSize_t(fnt->GetColor());
}
DEFINE_PYTHON_FUNCTION(
"F.GetZ() -> float\n"
"Returns the current font Z-order.",
METH_NOARGS,Font_GetZ)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetZ());
}
DEFINE_PYTHON_FUNCTION(
"F.GetBlendMode() -> int\n"
"Returns the current font blending mode.",
METH_NOARGS,Font_GetBlendMode)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyInt_FromLong((long)fnt->GetBlendMode());
}
DEFINE_PYTHON_FUNCTION(
"F.GetScale() -> float\n"
"Returns the current font scaling.",
METH_NOARGS,Font_GetScale)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetScale());
}
DEFINE_PYTHON_FUNCTION(
"F.GetProportion() -> float\n"
"Returns the current font proportion.",
METH_NOARGS,Font_GetProportion)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetProportion());
}
DEFINE_PYTHON_FUNCTION(
"F.GetRotation() -> float\n"
"Returns the current letters rotation.",
METH_NOARGS,Font_GetRotation)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetRotation());
}
DEFINE_PYTHON_FUNCTION(
"F.GetTracking() -> float\n"
"Returns the current font tracking.",
METH_NOARGS,Font_GetTracking)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetTracking());
}
DEFINE_PYTHON_FUNCTION(
"F.GetSpacing() -> float\n"
"Returns the current font line spacing.",
METH_NOARGS,Font_GetSpacing)
{
	hgeFont * fnt = Font_GetPtr(self);
	if (!fnt) return 0;
	return PyFloat_FromDouble((double)fnt->GetSpacing());
}
/*
GetSprite
GetPreWidth
GetPostWidth
GetStringWidth
GetHeight
*/
static PyMethodDef Font_methods[] = {
	{"Render",Font_Render,Font_Render_flag,Font_Render_doc},
	{"SetColor",Font_SetColor,Font_SetColor_flag,Font_SetColor_doc},
	{"SetZ",Font_SetZ,Font_SetZ_flag,Font_SetZ_doc},
	{"SetBlendMode",Font_SetBlendMode,Font_SetBlendMode_flag,Font_SetBlendMode_doc},
	{"SetScale",Font_SetScale,Font_SetScale_flag,Font_SetScale_doc},
	{"SetProportion",Font_SetProportion,Font_SetProportion_flag,Font_SetProportion_doc},
	{"SetRotation",Font_SetRotation,Font_SetRotation_flag,Font_SetRotation_doc},
	{"SetTracking",Font_SetTracking,Font_SetTracking_flag,Font_SetTracking_doc},
	{"SetSpacing",Font_SetSpacing,Font_SetSpacing_flag,Font_SetSpacing_doc},

	{"GetColor",Font_GetColor,Font_GetColor_flag,Font_GetColor_doc},
	{"GetZ",Font_GetZ,Font_GetZ_flag,Font_GetZ_doc},
	{"GetBlendMode",Font_GetBlendMode,Font_GetBlendMode_flag,Font_GetBlendMode_doc},
	{"GetScale",Font_GetScale,Font_GetScale_flag,Font_GetScale_doc},
	{"GetProportion",Font_GetProportion,Font_GetProportion_flag,Font_GetProportion_doc},
	{"GetRotation",Font_GetRotation,Font_GetRotation_flag,Font_GetRotation_doc},
	{"GetTracking",Font_GetTracking,Font_GetTracking_flag,Font_GetTracking_doc},
	{"GetSpacing",Font_GetSpacing,Font_GetSpacing_flag,Font_GetSpacing_doc},
	{NULL}  /* Sentinel */
};

int
pyInitFont(PyObject *mod)
{
	FontType.tp_new = Font_new;
	FontType.tp_dealloc = Font_dealloc;
	FontType.tp_init = Font_init;
	FontType.tp_repr = Font_repr;
	FontType.tp_str = Font_repr;
	FontType.tp_methods = Font_methods;
	if (PyType_Ready(&FontType) <0) return -1;
	Py_INCREF(&FontType);
	return PyModule_AddObject(mod, "Font", (PyObject *)&FontType); 
}